<template>
	<view class="funds-status_wrapper">
		<!-- 页面导航栏 -->
		<view class="nav_bar">
			<image src="https://pcdcoin.oss-cn-chengdu.aliyuncs.com/pcdApp/common/arrowLeftWhite.svg" class="nav_bar--icon" @click="jumpBack"></image>
			<view class="nav_bar--title">转入可提</view>
			<!-- {{showCheckBox?'完成':'批量'}} -->
			<view class="nav_bar--btn" @click="onBatchBtn"></view>
		</view>
		<view class="tab_bar">
			<view :class="{'tab-scroll_left':true,'tab_tab-active':tabActive === 1}" @click="onTabActive(1)">当前</view>
			<view :class="{'tab-scroll_right':true,'tab_tab-active':tabActive === 2}" @click="onTabActive(2)">历史</view>
		</view>
		<view v-if="addAgainRecordData.data.length !== 0">
			<view class="list_wrap">
				<view class="list_header">
					<checkbox-group @change="chooseAllFunds">
						<checkbox value="0" :checked="allCheckboxStatus" class="check_box" v-if="showCheckBox" />
					</checkbox-group>
					<text>数额(PCD)</text>
					<text>时间</text>
					<text>{{this.tabActive==2?"状态":"操作"}}</text>
				</view>
				<view class="list_content">
					<!-- <checkbox-group @change="chooseSingleFunds"> -->
					<!-- 当前列表 -->
					<view v-show="tabActive==1">
						<view class="list_content--item" v-for="item in addAgainRecordData.data" :key="item.id">
							<!-- <checkbox :value="item.id.toString()" :checked="singleCheckboxStatus[index]" class="check_box" v-if="showCheckBox" /> -->
							<text>{{$util.roundNumber(item.mining_amout,1)}}</text>
							<text>{{$util.FormatDate(item.create_time)}}</text>
							<view class="handle_btn" @click="setRevokeAgain(item.id)">
								<text>划转</text>
							</view>
						</view>
					</view>
					<!-- 历史列表 -->
					<view v-show="tabActive==2">
						<view class="list_content--item" v-for="item in addAgainRecordData.data" :key="item.id">
							<!-- <checkbox :value="item.id.toString()" :checked="singleCheckboxStatus[index]" class="check_box" v-if="showCheckBox" /> -->
							<text>{{$util.roundNumber(item.amount,2)}}</text>
							<text>{{$util.FormatDate(item.create_time)}}</text>
							<text>已入账可提资产</text>
						</view>
					</view>
					<!-- </checkbox-group> -->
				</view>
				<view class="list_content--footer" v-if="showCheckBox">
					<view>
						<text>总额：</text>
						<text class="mining_sum">{{fundsSum}}PCD</text>
					</view>
					<view>挖矿</view>
				</view>
			</view>
			<!-- 分页 -->
			<view class="pagination_wrap">
				<view class="pagination_left" @click="onPagination('previous')">
					<uni-icon type="back" color="#999999" size="20"></uni-icon>
				</view>
				<view class="pagination_text">{{currentPage + '/' + sumPage}}</view>
				<view class="pagination_right" @click="onPagination('next')">
					<uni-icon type="forward" color="#999999" size="20"></uni-icon>
				</view>
			</view>
		</view>
		<view class="content_null" v-else>
			暂无记录
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				menuActive: 0, // 当前选中菜单
				addAgainRecordData: {
					data:[]
				}, // 当前列表数据
				showCheckBox: false, // 显示多选框
				chooseFundsList: [], // 选中的可挖矿ID
				allCheckboxStatus: false, // 所有多选框状态
				singleCheckboxStatus: [], // 单个多选框状态
				fundsSum: 0, // 资金总额
				currentPage: 1, // 当前页
				limitPage: 10, // 每页展示条数
				tabActive: 1, //给tab栏添加样式
				// status:1,
			}
		},
		computed: {
			sumPage() {
				return this.addAgainRecordData.count ? Math.ceil(this.addAgainRecordData.count / this.limitPage) : 1
			}
		},
		onReady() {
			this.addAgainRecord()
		},
		methods: {
			// 监听tab菜单
			onTabActive(val){
				if(val===1){
					this.addAgainRecord()
				}else{
					this.addAgainRevoke()
				}
				this.tabActive = val;
				this.currentPage = 1
			},
			// 复投列表
			addAgainRecord() {
				uni.showLoading({
					title: "加载中"
				});
				this.$api.addAgainRecord({
					limit: this.limitPage,
					page: this.currentPage
				}, res => {
					uni.hideLoading();
					if (res.data.type === 'ok') {
						this.addAgainRecordData = res.data.message
						// this.fundsListData.data.forEach((item, index) => {
						// 	this.singleCheckboxStatus.push(false)
						// })
					}
				})
			},
			// 划转历史列表
			addAgainRevoke() {
				uni.showLoading({
					title: "加载中"
				});
				this.$api.addAgainRevoke({
					limit: this.limitPage,
					page: this.currentPage
				}, res => {
					uni.hideLoading();
					if (res.data.type === 'ok') {
						this.addAgainRecordData = res.data.message
						// this.fundsListData.data.forEach((item, index) => {
						// 	this.singleCheckboxStatus.push(false)
						// })
					}
				})
			},
			// 监听划转按钮
			setRevokeAgain(id) {
				console.log(id)
				this.$api.setRevokeAgain({
					id: id
				}, res => {
					if (res.data.type === 'ok') {
						this.addAgainRecord()
					}
					uni.showToast({
						title: res.data.message,
						icon: 'none'
					})
				})
			},
			// 监听批量按钮
			onBatchBtn() {
				// 如果showCheckBox===false,按钮显示批量，清空选中状态
				if (this.showCheckBox) {
					this.showCheckBox = false

				} else {
					this.showCheckBox = true
					this.allCheckboxStatus = false
				}
			},
			// 选择单独可挖矿
			chooseSingleFunds(val) {
				// 拿不到列表的index，只能去循环判断
				this.fundsListData.data.forEach((res, resIndex) => {
					val.detail.value.forEach((item, itemIndex) => {
						if (res.id === parseInt(item)) {
							this.singleCheckboxStatus[resIndex] = true
						} else {
							this.singleCheckboxStatus[resIndex] = false
						}
					})
				})
				// this.chooseFundsList = val.detail.value

				// 当前数组不等于当前总数据，说明没有全选
				const listLength = val.detail.value.length
				if (listLength !== this.fundsListData.data.length) {
					// 取消全选状态
					this.allCheckboxStatus = false
				} else {
					this.allCheckboxStatus = true
				}
				// todo 选择前和选择后分别加减总量
			},
			// 选择所有可挖矿
			chooseAllFunds(val) {
				if (val.detail.value.length !== 0) {
					this.fundsListData.data.forEach((item, index) => {
						// 单个多选框状态
						this.singleCheckboxStatus[index] = true
						// 加总额
						this.fundsSum += parseInt(item.lock_amount)
					})
					// 选中所有的数据
					this.chooseFundsList = this.fundsListData.data
					// 选中所有按钮
					this.allCheckboxStatus = true
				} else {
					this.fundsListData.data.forEach((item, index) => {
						this.singleCheckboxStatus[index] = false
					})
					// 清空选中列表
					this.chooseFundsList = []
					this.allCheckboxStatus = false
					// 总额清0
					this.fundsSum = 0
				}
			},
			// 分页
			onPagination(arg) {
				if (arg === "previous") {
					if (this.currentPage > 1) {
						this.currentPage--
					} else {
						return
					}
				} else {
					if (this.currentPage < this.sumPage) {
						this.currentPage++
					} else {
						uni.showToast({
							title: '没有更多了',
							icon: 'none'
						})
						return
					}
				}
				if (this.tabActive == 1) {
					this.addAgainRecord();
				} else {
					this.addAgainRevoke()
				}
			},
			// 返回
			jumpBack() {
				uni.navigateBack({
					delta: 1
				});
			}
		}
	}
</script>

<style lang="scss" scoped>
	@mixin flex-layout {
		display: flex;
		justify-content: space-around;
		align-items: center;
	}

	.funds-status_wrapper {
		height: 100vh;
		background: #FFFFFF;
	}
	.nav_bar {
		position: fixed;
		top: 0;
		left: 0;
		display: flex;
		justify-content: space-between;
		width: 100%;
		height: 140upx;
		line-height: 200upx;
		box-shadow: 2upx 0 10upx rgba(0, 0, 0, .2);
		background-color: #25263D;
		.nav_bar--icon{
			width:30upx;
			height:30upx;
			margin:90upx 0 0 40upx;
		}
		.nav_bar--title {
			margin-right: 40upx;
			font-size: 15px;
			color: #FFFFFF;
		}
		.nav_bar--btn {
			margin-right: 20upx;
		}
	}

	.list_wrap {
		color: #333333;
		.list_header {
			@include flex-layout;
			height: 87upx;
			background-color: #EEEEEE;

			.check_box {
				transform: scale(0.7);
			}

			text {
				width: 33.33%;
				text-align: center;
			}
		}

		.list_content {
			.list_content--item {
				height: 87upx;
				@include flex-layout;

				.check_box {
					transform: scale(0.7);
				}

				text {
					flex: 1;
					text-align: center;
				}

				.handle_btn {
					flex: 1;
					text-align: center;

					text {
						padding: 10upx 30upx;
						border-radius: 30upx;
						background-color: #E13E4D;
						color: #FFFFFF;
					}
				}
			}
		}

		.list_content--footer {
			position: fixed;
			bottom: 0;
			left: 0;
			height: 100upx;
			line-height: 100upx;
			width: 100%;
			box-shadow: 0 5upx 10upx rgba(0, 0, 0, .5);
			font-size: 15px;

			view:first-child {
				float: left;
				margin-left: 20upx;

				.mining_sum {
					color: #E13E4D;
				}
			}

			view:last-child {
				float: right;
				width: 30%;
				text-align: center;
				background-color: #E13E4D;
				color: #FFFFFF;
			}
		}
	}

	.pagination_wrap {
		display: flex;
		justify-content: space-between;
		align-items: center;
		width: 80%;
		position: fixed;
		bottom: 100upx;
		left: 12%;

		.pagination_left,
		.pagination_right {
			padding: 0 10upx;
			border-radius: 10upx;
			border: 1px solid #BBBBBB;
		}
	}

	.content_null {
		height: 100%;
		display: flex;
		justify-content: center;
		align-items: center;
		font-size: 18px;
	}

	.tab_bar {
		display: flex;
		height: 100upx;
		line-height: 100upx;
		background: #25263D;
		margin-top: 130upx;
		.tab-scroll_left,.tab-scroll_right {
			text-align: center;
			color: #fff;
			width: 50%;
		}
		.tab_tab-active {
			color: #E13E4D;
			border-bottom: 4upx solid #E13E4D;
		}

	}
</style>
